


//ws
// export const WS = ()=>{
//   var ws = new WebSocket("wss://echo.websocket.org");

//   ws.onopen = function(evt) { 
//     console.log("Connection open ...",evt); 
//     ws.send("Hello WebSockets!");
//   };

//   ws.onmessage = function(evt) {
//     console.log( "Received Message: " + evt.data);
//     ws.close();
//   };

//   ws.onclose = function() {
//     console.log("Connection closed.",);
//   }; 

//   ws.onerror = function() {
//     console.log("error!!!"); 
//   }; 
// }

//
export const subtractMinutes=(time, minutes)=> {
  // console.log("咋了",time)
  if(time ==[]||!time){
    return '05:00'
  }
  // console.log("没了",time)
  time = time.toString()
  const [hour, minute] = time?.split(":")
  const date = new Date();
  date.setHours(Number(hour));
  date.setMinutes(Number(minute) - Number(minutes));
  return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
}

//获取URL_key
export const getUrlParams = (url)=> {
  // 通过 ? 分割获取后面的参数字符串
  let urlStr = url.split('?')
 
  if(urlStr.length<2){
    return null;
  }else{
    urlStr=urlStr[1]
  }
    // 创建空对象存储参数
  let obj = {};
    // 再通过 & 将每一个参数单独分割出来
  let paramsArr = urlStr.split('&')
  for(let i = 0,len = paramsArr.length;i < len;i++){
        // 再通过 = 将每一个参数分割为 key:value 的形式
    let arr = paramsArr[i].split('=')
    obj[arr[0]] = arr[1];
  }
  return obj
}
export const getUrlParamsByJs = (url)=>{
  let src = new URL(url)
  let param = URLSearchParams(src.searchParams)
  console.log(param)
  return param
}
//去除空格
export const clearSpace = (val)=>{
  if(val){
    return val.replace(/\s*/g,"")
  }else{
    return val
  }
}
//调用确认提示
export const opneConfirm = async ()=>{
  window.systemService.confirm({
    title: '确定要执行该操作吗？'
  }).then(confirm => {
    if (!confirm) {
        console.log('取消了');
        return;
    }
    console.log('确认了');
  });

}

//调用数据表服务
export const getDetailById = async (staff,id)=>{
  return window.tableService.queryRecordById(staff,id).then((info)=>{
    return info
    // {table:{},tableFieldList:[]...}
  })
}

//调用织信自动化接口
export const ajaxUseAuto = async (auto_id,option) => {
    // console.log('option',option,'auto_id',auto_id)
    // let data = await window.systemService.callAutomatic({
    //     automaticId: auto_id,
    //     args: [option]
    // })
    // console.log(data)
    // return dataList;

    return window.systemService.callAutomatic({
          automaticId: auto_id,
          args: [option]
    }).then((result) => {
      // console.log('自动化',result)
      var returnval  = null
      if(result.returnValue){
        returnval = result.returnValue
      }else{
        returnval = true
      }
        return returnval
    }).catch(()=>{
      return false
    })
   
}

//数据格式化
export const formatData = (list,optionList)=>{
    optionList
    list.forEach(item =>{
        item.status = optionList[0]
    } )
}


//api请求
const axios = require('axios').default;
export const ajax = axios.create({
    baseURL: 'https://uat2.informat.cn/web0/api/y2jtobhqycwxb/',
    timeout: 1000,
    headers: {'X-Custom-Header': 'foobar'}
  });

//   https://next.informat.cn/web0/api/y2jtobhqycwxb/getList

//时间格式
export function formatDate(dateVal,type) {
  if(!(dateVal instanceof Date)){
    dateVal = new Date(dateVal)
  }
  var year = dateVal.getFullYear(),
      month = dateVal.getMonth() + 1,
      date = dateVal.getDate(),
      hour = dateVal.getHours().toString().padStart(2, '0'),
      minute = dateVal.getMinutes().toString().padStart(2, '0');
  let dataTime =null;  
  switch(type){
  
    case 'year': dataTime =year + "-" + month + "-" + date;break;
    case 'month': dataTime = month + "-" + date;break;
    case 'date': dataTime = date + " " + hour + ":" + minute;break;
    case 'time': dataTime = hour + ":" + minute;break;
    default:dataTime =`${year}-${month}-${date} ${hour}:${minute}`;break;

  }
  return dataTime
}

//计算时间差
export const getTimeDiffInMinutes= (dateVal)=> {
  if(!(dateVal instanceof Date)){
    dateVal = new Date(dateVal)
  }
  const diffInMilliseconds =  new Date()-dateVal;
  const diffInMinutes = Math.floor(diffInMilliseconds / (1000 * 60));

  return diffInMinutes;
}

export const extractJsonFromString=(str)=> {
  const start = str.indexOf('{'); // 找到左花括号的位置
  const end = str.lastIndexOf('}'); // 找到右花括号的位置
  const jsonString = str.slice(start, end + 1); // 截取大括号内的字符串
  const jsonObj = JSON.parse(jsonString); // 将字符串解析为 JSON 对象
  return jsonObj;
}